<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts 主体
import resize from './mixins/resize'
import { ActiveNumStatistic } from "@/api/dashboard.js";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    btDat: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  watch: {
    btDat(name, value) {
      console.log('传过来的数据---流量', name)
      this.lineDate = name
      if (name.userTotal > name.monthFlow) {
        // this.lineDate.monthFlow = 0
        this.itemColor = '#ff7995'
      }
      this.initChart()
    }
  },
  data() {
    return {
      chart: null,
      lineDate: {},
      itemColor: '#60adff',
      fluxColor: '#38464e'
    }
  },
  created() {
    //this.getActiveNumStatistic()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {

    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        //  backgroundColor: 'rgba(1,202,217,.2)',
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} GB ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['当月可使用流量', '当月已使用流量'],
          textStyle: {
            color: 'rgba(255,255,255,.7)'
          },
        },
        series: [
          {
            name: '流量统计',
            type: 'pie',
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center',
              formatter: ['{b}: ', '( {c}G )'].join(''),
              color: '#fff'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '20',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: [
              { value: this.lineDate.monthFlow, name: '当月可使用流量', itemStyle: { color: this.fluxColor }},
              { value: this.lineDate.userTotal, name: '当月已使用流量', itemStyle: { color: this.itemColor } }
            ],
            xAxis: {
              axisLabel: { show: false }
            },
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}

</script>
